﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Common;
using RedHouse.Data.Entities;

namespace RedHouse.Data.Base
{
    public class DishService : IDishService
    {
        #region Declaration
        private NewRedHouseEntitiesDataContext _dataContext;
        #endregion

        public DishService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region IDishService Members

        public Common.OperationResult SaveDish(RedHouseDish dish)
        {
            OperationResult result = new OperationResult()
            {
                Result = true,
                Message = string.Empty
            };
            try
            {
                Dish savedDish = new Dish()
                {
                    ID = dish.Id,
                    LocationID = dish.LocationID,
                    CategoryID = dish.CategoryID,
                    Rating = dish.Rating,
                    CreatedBy = dish.CreatedBy,
                    CreatedDate = dish.CreatedDate
                };

                Detail dishDetail = new Detail()
                {
                    ID = Guid.NewGuid(),
                    ImageUrl = dish.ImageUrl,
                    LangID = dish.LangId,
                    Description = dish.Description,
                    CreatedBy = dish.CreatedBy,
                    CreatedDate = dish.CreatedDate,
                    AssociatedID = dish.Id,
                    Title = dish.Title
                };

                _dataContext.Dishes.InsertOnSubmit(savedDish);
                _dataContext.Details.InsertOnSubmit(dishDetail);
                _dataContext.SubmitChanges();

            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public Common.OperationResult DeleteDish(Guid id)
        {
            OperationResult result = new OperationResult()
            {
                Result = true,
                Message = string.Empty
            };
            try
            {
                Dish dish = _dataContext.Dishes.Where(d => d.ID == id).FirstOrDefault();
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == id).FirstOrDefault();
                List<Image> images = _dataContext.Images.Where(i => i.AssociatedID == id).ToList();
                _dataContext.Images.DeleteAllOnSubmit(images);
                _dataContext.Details.DeleteOnSubmit(detail);
                _dataContext.Dishes.DeleteOnSubmit(dish);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public List<RedHouseDish> GetAllDish()
        {
            List<Dish> dishList = _dataContext.Dishes.ToList();
            List<RedHouseDish> retList = new List<RedHouseDish>();

            foreach (Dish dish in dishList)
            {
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == dish.ID).Single();
                Detail categoryDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.CategoryID).Single();
                RedHouseDish item = new RedHouseDish()
                {
                    Id = dish.ID,
                    LocationID = dish.LocationID.HasValue ? dish.LocationID.Value : 0,
                    Rating = dish.Rating.HasValue ? dish.Rating.Value : 0,
                    CategoryID = dish.CategoryID.HasValue ? dish.CategoryID.Value : Guid.NewGuid(),
                    CreatedDate = dish.CreatedDate.HasValue ? dish.CreatedDate.Value : DateTime.Now,
                    CreatedBy = dish.CreatedBy,
                    Type = dish.Type,
                    Title = detail.Title,
                    ImageUrl = detail.ImageUrl,
                    LangId = detail.LangID.HasValue ? detail.LangID.Value : Guid.NewGuid(),
                    Description = detail.Description,
                    Group = detail.GroupType.HasValue ? detail.GroupType.Value : 0,
                    ShortDesc = detail.Description.Length > 200 ? string.Format("{0}...", detail.Description.Substring(0, 200)) : detail.Description,
                    Category = categoryDetail.Title
                };
                retList.Add(item);
            }
            return retList;
        }


        public List<RedHouseDish> GetAllDishByCategory(Guid categoryId)
        {
            List<Dish> dishList = _dataContext.Dishes.Where(d=>d.CategoryID==categoryId).ToList();
            List<RedHouseDish> retList = new List<RedHouseDish>();

            foreach (Dish dish in dishList)
            {
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == dish.ID).Single();
                Detail categoryDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.CategoryID).Single();
                RedHouseDish item = new RedHouseDish()
                {
                    Id = dish.ID,
                    LocationID = dish.LocationID.HasValue ? dish.LocationID.Value : 0,
                    Rating = dish.Rating.HasValue ? dish.Rating.Value : 0,
                    CategoryID = dish.CategoryID.HasValue ? dish.CategoryID.Value : Guid.NewGuid(),
                    CreatedDate = dish.CreatedDate.HasValue ? dish.CreatedDate.Value : DateTime.Now,
                    CreatedBy = dish.CreatedBy,
                    Type = dish.Type,
                    Title = detail.Title,
                    ImageUrl = detail.ImageUrl,
                    LangId = detail.LangID.HasValue ? detail.LangID.Value : Guid.NewGuid(),
                    Description = detail.Description,
                    Group = detail.GroupType.HasValue ? detail.GroupType.Value : 0,
                    ShortDesc = detail.Description.Length > 200 ? string.Format("{0}...", detail.Description.Substring(0, 200)) : detail.Description,
                    Category = categoryDetail.Title
                };
                retList.Add(item);
            }
            return retList;
        }

        #endregion


        #region IDishService Members


        public RedHouseDish GetDishById(Guid id)
        {
            RedHouseDish retDish = new RedHouseDish();
            Dish dish = _dataContext.Dishes.Where(d => d.ID == id).FirstOrDefault();
            if (dish != null)
            {
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == dish.ID).Single();
                Detail categoryDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.CategoryID).Single();
                retDish = new RedHouseDish()
                {
                    Id = dish.ID,
                    LocationID = dish.LocationID.HasValue ? dish.LocationID.Value : 0,
                    Rating = dish.Rating.HasValue ? dish.Rating.Value : 0,
                    CategoryID = dish.CategoryID.HasValue ? dish.CategoryID.Value : Guid.NewGuid(),
                    CreatedDate = dish.CreatedDate.HasValue ? dish.CreatedDate.Value : DateTime.Now,
                    CreatedBy = dish.CreatedBy,
                    Type = dish.Type,
                    Title = detail.Title,
                    ImageUrl = detail.ImageUrl,
                    LangId = detail.LangID.HasValue ? detail.LangID.Value : Guid.NewGuid(),
                    Description = detail.Description,
                    Group = detail.GroupType.HasValue ? detail.GroupType.Value : 0,
                    ShortDesc = detail.Title.Length > 200 ? string.Format("{0}...", detail.Title.Substring(0, 200)) : detail.Title,
                    Category = categoryDetail.Title
                };
            }
            return retDish;
        }

        public OperationResult UpdateDish(RedHouseDish dish)
        {
            OperationResult result = new OperationResult()
            {
                Result = true,
                Message = string.Empty
            };
            try
            {
                Dish savedDish  = _dataContext.Dishes.Where(d => d.ID == dish.Id).FirstOrDefault();
                
                    savedDish.LocationID = dish.LocationID;
                    //savedDish.CategoryID = dish.CategoryID;
                    savedDish.Rating = dish.Rating;
                    savedDish.CreatedBy = dish.CreatedBy;
                    savedDish.CreatedDate = dish.CreatedDate;

                    Detail dishDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.Id).FirstOrDefault();
                
                    dishDetail.ImageUrl = dish.ImageUrl;
                    dishDetail.LangID = dish.LangId;
                    dishDetail.Description = dish.Description;
                    dishDetail.CreatedBy = dish.CreatedBy;
                    dishDetail.CreatedDate = dish.CreatedDate;
                    dishDetail.Title = dish.Title;

                //_dataContext.Dishes.Attach(savedDish);
                //_dataContext.Details.Attach(dishDetail);
                _dataContext.SubmitChanges();

            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = ex.Message;
            }
            return result;
        }

        #endregion
    }
}
